package showUser;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.User;
import dao.UserDao;
import dao.impl.UserDaoImpl;

/**
 * Servlet implementation class Show
 */
public class Show extends HttpServlet {
	private static final long serialVersionUID = 1L;
	UserDao userDao=new UserDaoImpl();   
    public Show() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<User> users=userDao.findAll();
        for (User user : users) {
			System.out.println(user);
		}
		response.setContentType("text/html;charset=utf-8");
		StringBuilder sBuilder=new StringBuilder();
		sBuilder.append("<table >");
		sBuilder.append("<thead>");
		sBuilder.append("<tr><th>id</th><th>name</th><th>salary</th><th>birth</th><th>sex</th></tr>");
		sBuilder.append("</thead>");
		for(int i=0;i<users.size();i++)
		{
			sBuilder.append("<tr>");
			sBuilder.append("<td>");
			sBuilder.append(users.get(i).getId());
			sBuilder.append("</td>");
			sBuilder.append("<td>");
			sBuilder.append(users.get(i).getName());
			sBuilder.append("</td>");
			sBuilder.append("<td>");
			sBuilder.append(users.get(i).getSalary());
			sBuilder.append("</td>");
			sBuilder.append("<td>");
			sBuilder.append(users.get(i).getBirth());
			sBuilder.append("</td>");
			sBuilder.append("<td>");
			if(users.get(i).isSex())
			{
				sBuilder.append("男");
			}else {
				sBuilder.append("女");
			}
			sBuilder.append("</td>");
			sBuilder.append("</tr>");
		}
		sBuilder.append("</table>");
		response.getWriter().write(sBuilder.toString());
	}

}
